// miniprogram/pages/record/add.js
Page({
  data: {
    rules: [],
    selectedRule: null,
    points: '',
    description: '',
    sceneType: '',
    auditorInfo: [] // 添加审核人信息数据
  },

  onLoad() {
    const app = getApp()
    this.setData({
      sceneType: app.globalData.sceneType
    })
    this.loadRules()
    this.loadAuditorInfo() // 加载审核人信息
  },

  loadRules() {
    // 模拟加载规则数据
    const rules = [
      { id: 1, name: '完成家务', points: 10 },
      { id: 2, name: '早睡早起', points: 5 },
      { id: 3, name: '运动健身', points: 15 },
      { id: 4, name: '迟到', points: -5 },
      { id: 5, name: '忘记重要事情', points: -10 }
    ]
    
    this.setData({ rules })
  },

  // 加载审核人信息
  loadAuditorInfo() {
    const { sceneType } = this.data
    let auditorInfo = []
    
    // 根据不同场景类型设置审核人信息
    if (sceneType === 'company') {
      // 公司场景，多级审核人
      auditorInfo = [
        { name: '直属主管', role: '部门经理', level: 1, avatar: '/images/avatar.png' },
        { name: '人事部', role: '人事经理', level: 2, avatar: '/images/avatar.png' },
        { name: '总经理', role: '公司领导', level: 3, avatar: '/images/avatar.png' }
      ]
    } else {
      // 非公司场景，单个审核人
      auditorInfo = [
        { name: '审核人', role: '家庭成员', level: 1, avatar: '/images/avatar.png' }
      ]
    }
    
    this.setData({ auditorInfo })
  },

  selectRule(e) {
    const index = e.detail.value
    const selectedRule = this.data.rules[index]
    this.setData({
      selectedRule: selectedRule,
      points: selectedRule.points
    })
  },

  onInput(e) {
    const field = e.currentTarget.dataset.field
    const value = e.detail.value
    this.setData({
      [field]: value
    })
  },

  submitRecord() {
    const { selectedRule, points, description } = this.data
    
    if (!selectedRule) {
      wx.showToast({
        title: '请选择积分规则',
        icon: 'none'
      })
      return
    }
    
    if (!points) {
      wx.showToast({
        title: '请输入积分值',
        icon: 'none'
      })
      return
    }
    
    if (!description) {
      wx.showToast({
        title: '请输入说明',
        icon: 'none'
      })
      return
    }
    
    // 模拟提交操作
    wx.showToast({
      title: '提交成功',
      icon: 'success'
    })
    
    setTimeout(() => {
      wx.navigateBack()
    }, 1500)
  }
})